import RPi.GPIO as GPIO
import time

# use physical pin numbers
GPIO.setmode(GPIO.BOARD)

# switch off GPIO warnings
GPIO.setwarnings(False)

def init():
  # setup output pin, switch pin off
  GPIO.setup(3,GPIO.OUT,initial=0)  

def description():
  print('Optical Transmitter pidget methods:')
  print('  init():                 initializes output pin')
  print('  set_laser([0,1]):       sets laser to on or off')
  print('  set_laserpulse(length): switches laser on for length seconds')
  print('  get_state():            returns state of laser as boolean')
  print('  test():                 flashes laser for 1 second')

def set_laser(state):
  try:
    GPIO.output(3,int(state))
  except ValueError:
    print ('Error: state is not an integer')
    
def set_laserpulse(length):
  try:
    length=float(length)
    GPIO.output(3,1)
    time.sleep(length)
    GPIO.output(3,0)
  except ValueError:
    print ('Error: length is not an number')
    
def get_state():
  return GPIO.input(3)

def test():
  GPIO.output(3,1)
  time.sleep(1)
  GPIO.output(3,0)
